package testJdk.commonUse.string.el.spel;

import cn.hutool.extra.expression.engine.spel.SpELEngine;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0.0
 * Created by cyd on 2023-06-21 09:25:42
 * @ClassName : learn_jdk.commonUse.string.el.spel.TestSpel
 * @Description : https://blog.csdn.net/weixin_39651356/article/details/124611374
 */
public class TestSpel {

    String spStr = "{\"params\":{\"breaker_id\": #breaker_id,\"breaker_switch_state\": #breaker_switch_state},\"serviceIdentifier\":\"breaker_switch_cmd\"}";
    SpelExpressionParser spelExpressionParser = new SpelExpressionParser();


    /**
     * 原生spel
     */
    @Test
    public void test01() {
        StandardEvaluationContext context = new StandardEvaluationContext();

        Map<String, Object> map = new HashMap<>();
        map.put("breaker_id", 1);
        map.put("breaker_switch_state", 2);

        context.setVariables(map);
        Expression expression = spelExpressionParser.parseExpression(spStr);

        Map values = expression.getValue(context, Map.class);
        System.out.println(values.keySet());
        System.out.println(values.values());
    }


    /**
     * hutool的el工具
     */
    @Test
    public void test() {
        SpELEngine spELEngine = new SpELEngine();
        Map<String, Object> map = new HashMap<>();
        map.put("breaker_id", 1);
        map.put("breaker_switch_state", 2);
        Object eval = spELEngine.eval(spStr, map);
        System.out.println(eval.toString());
    }

}
